package com.pocketsweet.ui.uilib;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Scroller;
import android.widget.TextView;

import com.pocketsweet.MLApplication;
import com.pocketsweet.R;
import com.pocketsweet.ui.fragments.HomeFragment;
import com.pocketsweet.ui.fragments.NewOnlineFragment;
import com.pocketsweet.ui.fragments.TempHomeFragment;
import com.pocketsweet.ui.fragments.TempNewOnlineFragment;

public class SlideView extends LinearLayout {

    private static final String TAG = "SlideView";

    private Context mContext;
    private LinearLayout mViewContent;
    private RelativeLayout mHolder;
    private Scroller mScroller;
    private OnSlideListener mOnSlideListener;
    public boolean isScroll = true;

    private int mHolderWidth;
    boolean isMove = false;

    private int mLastX = 0;
    private int mLastY = 0;
    private static final int TAN = 2;

    public interface OnSlideListener {
        public static final int SLIDE_STATUS_OFF = 0;
        public static final int SLIDE_STATUS_START_SCROLL = 1;
        public static final int SLIDE_STATUS_ON = 2;

        /**
         * @param view   current SlideView
         * @param status SLIDE_STATUS_ON or SLIDE_STATUS_OFF
         */
        public void onSlide(View view, int status);
    }

    public SlideView(Context context) {
        super(context);
        initView();
    }

    public SlideView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        mContext = getContext();
        mScroller = new Scroller(mContext);
        mHolderWidth = getResources().getInteger(
                R.integer.onLineContainerRecommendWidth);

        setOrientation(LinearLayout.HORIZONTAL);
        View.inflate(mContext, R.layout.view_slide, this);
        mViewContent = (LinearLayout) findViewById(R.id.view_content);
        mHolderWidth = Math.round(TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, mHolderWidth, getResources()
                        .getDisplayMetrics()));
    }

    public void setButtonText(CharSequence text) {
        ((TextView) findViewById(R.id.tvsee)).setText(text);
    }

    public void setContentView(View view) {
        mViewContent.addView(view);
    }

    public void setOnSlideListener(OnSlideListener onSlideListener) {
        mOnSlideListener = onSlideListener;
    }

    public void shrink() {
        if (getScrollX() != 0) {
            this.smoothScrollTo(0, 0);
            if (MLApplication.getInstance().isLogin == true) {

                NewOnlineFragment.isSlide = false;
            } else {
                TempNewOnlineFragment.isSlide = false;
            }
        }
    }

    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                           float velocityY) {
        int kEvent;
        if (isScrollingLeft(e1, e2)) {
            kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
        } else {
            kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
        }
        onKeyDown(kEvent, null);
        return true;

    }

    private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
        return e2.getX() > (e1.getX() + 50);
    }

    public void onRequireTouchEvent(MotionEvent event) {
        if (isScroll == false) {
            if (MLApplication.getInstance().isLogin == true) {
                HomeFragment.mPager.setScrollble(true);
            } else {
                if (TempHomeFragment.mPager != null) {
                    TempHomeFragment.mPager.setScrollble(true);
                }

            }
        } else {
            int startX = (int) event.getX();
            int y = (int) event.getY();
            int scrollX = getScrollX();
            // Log.d(TAG, "x=" + startX + "  y=" + y);

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {

                    if (!mScroller.isFinished()) {
                        mScroller.abortAnimation();
                    }
                    if (mOnSlideListener != null) {
                        mOnSlideListener.onSlide(this,
                                OnSlideListener.SLIDE_STATUS_START_SCROLL);
                    }
                    break;
                }
                case MotionEvent.ACTION_MOVE: {
                    this.requestDisallowInterceptTouchEvent(true);
                    isMove = true;
                    int deltaX = startX - mLastX;
                    int deltaY = y - mLastY;
                    if (Math.abs(deltaX) < Math.abs(deltaY) * TAN) {
                        break;
                    }

                    int newScrollX = scrollX - deltaX;
                    if (deltaX != 0) {
                        if (newScrollX < 0) {
                            newScrollX = 0;
                        } else if (newScrollX > mHolderWidth) {
                            newScrollX = mHolderWidth;
                        }
                        this.scrollTo(newScrollX, 0);
                    } else {
                    }
                    break;
                }
                case MotionEvent.ACTION_UP: {
                    if (isMove == true) {
                        if (MLApplication.getInstance().isLogin == true) {

                            NewOnlineFragment.isSlide = true;
                        } else {
                            TempNewOnlineFragment.isSlide = true;
                        }
                    } else {
                        if (MLApplication.getInstance().isLogin == true) {

                            NewOnlineFragment.isSlide = false;
                        } else {
                            TempNewOnlineFragment.isSlide = false;
                        }
                    }
                    int newScrollX = 0;
                    if (scrollX - mHolderWidth * 0.75 > 0) {
                        newScrollX = mHolderWidth;
                    }
                    if (isMove && scrollX > 0) { // 如果是滑动，则设置isSlide是true，取消点击事件
                        if (MLApplication.getInstance().isLogin == true) {

                            NewOnlineFragment.isSlide = true;
                        } else
                            TempNewOnlineFragment.isSlide = true;
                    } else if (scrollX == 0) {
                        if (MLApplication.getInstance().isLogin == true)

                            NewOnlineFragment.isSlide = false;
                        else
                            TempNewOnlineFragment.isSlide = false;
                    }
                    this.smoothScrollTo(newScrollX, 0);
                    if (mOnSlideListener != null) {
                        mOnSlideListener.onSlide(this,
                                newScrollX == 0 ? OnSlideListener.SLIDE_STATUS_OFF
                                        : OnSlideListener.SLIDE_STATUS_ON);
                        // if(newScrollX==OnSlideListener.SLIDE_STATUS_OFF){
                        // OnlineTestLoverActivity.isSlide = false;
                        // }
                    }
                    break;
                }
                default:

                    break;
            }

            mLastX = startX;
            mLastY = y;
        }

    }

    private void smoothScrollTo(int destX, int destY) {
        // 缓慢滚动到指定位置
        int scrollX = getScrollX();
        int delta = destX - scrollX;
        mScroller.startScroll(scrollX, 0, delta, 0, Math.abs(delta) * 3);
        invalidate();
        this.requestDisallowInterceptTouchEvent(true);
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            postInvalidate();
        }
    }

}
